package com.chenyue.cm.member.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.common.constant.ErrorCode;
import com.chenyue.cm.common.dao.JdAboutUsMapper;
import com.chenyue.cm.exception.SysRuntimeException;
import com.chenyue.cm.member.dao.JdMemberMapper;
import com.chenyue.cm.member.domain.JdMember;
import com.chenyue.cm.member.service.JdMemberService;
import com.chenyue.cm.member.vo.MemberVo;
import com.chenyue.cm.order.domain.JdProduct;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;


/**
 * @author chenyue-cm
 */
@Service(value = "JdMemberService")
public class JdMemberServiceImpl extends BaseServiceImpl<JdMember> implements JdMemberService {

    private final JdMemberMapper jdMemberMapper;

    private final JdAboutUsMapper aboutUsMapper;

    public JdMemberServiceImpl(@Qualifier("jdMemberMapper") JdMemberMapper jdMemberMapper, @Qualifier("jdAboutUsMapper") JdAboutUsMapper aboutUsMapper) {
        this.jdMemberMapper = jdMemberMapper;
        this.aboutUsMapper = aboutUsMapper;
    }

    @Override
    @Resource(name = "jdMemberMapper")
    public void setBaseMapper(LcBaseMapper<JdMember, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    /**
     * 获取会员
     *
     * @param id 用户id
     * @return 封装vo
     */
    @Override
    public MemberVo getMember(Integer id) {
        JdMember member = jdMemberMapper.selectOne(
                new QueryWrapper<JdMember>().lambda()
                        .eq(JdMember::getUserId, id)
                        .orderByDesc(JdMember::getExpireTime)
                        .last(" limit 1 ")
        );

        MemberVo result = new MemberVo();
        if (member != null) {
            BeanUtils.copyProperties(member, result);
            if (result.getExpireTime().getTime() <= System.currentTimeMillis()) {
                result.setIsMember(2);
            } else {
                result.setIsMember(1);
            }
        }

        result.setContent(aboutUsMapper.selectById("3").getContent());
        return result;
    }

    /**
     * 会员充值
     *
     * @param userId  用户id
     * @param product 商品
     * @param orderId 订单id
     */
    @Override
    public void rechargeMember(Integer userId, JdProduct product, Integer orderId) {
        MemberVo member = getMember(userId);
        JdMember newMember = new JdMember();
        newMember.setCt(Calendar.getInstance().getTime());
        newMember.setUserId(userId);
        newMember.setGetType(1);
        newMember.setLevel(1);
        newMember.setOrderId(orderId);
        if (member.getIsMember() == 1) {
//            已经是会员 走续费
            newMember.setExpireTime(getExpireTime(member.getExpireTime(), product.getTimeType()));
        } else {
//            不是会员 or 会员已过期 走开通
            newMember.setExpireTime(getExpireTime(null, product.getTimeType()));
        }
        int success = jdMemberMapper.insert(newMember);

        if (success == 0) {
            throw new SysRuntimeException(ErrorCode.INSERT_ERROR.getMsg());
        }
    }

    /**
     * 获取会员过期时间
     *
     * @param current  当前过期时间 为null则取当前
     * @param timeType 时间类型 123月季年
     * @return 过期时间
     */
    private Date getExpireTime(Date current, int timeType) {
        Date now = Calendar.getInstance().getTime();
        if (current == null) {
            current = now;
        }

        Date result = now;

        switch (timeType) {
            case 1:
//                一个月
                result = DateUtil.offsetMonth(current, 1);
                break;
            case 2:
//                一个季度
                result = DateUtil.offsetMonth(current, 3);
                break;
            case 3:
//                一年
                result = DateUtil.offsetMonth(current, 12);
                break;
            default:
                break;
        }

        return result;
    }
}
